define(['jquery'], function() {
  class Header {
    constructor () {
      this.loadHTML().then(() => {
        this.login();
        this.regis();
        this.logout();
        this.navClick();
      })
    }

    loadHTML () {
      return new Promise((resolve,reject) => {
        $('header').load('/html/modules/header.html', () => {
          this.isCookie()
          resolve()
        })
      })
    }

    login () {
      $('.right-login a').eq(0).on('click', () => {
        location.assign('http://localhost:8081/html/login.html?login')
      })
    }
    regis () {
      $('.right-login a').eq(1).on('click', () => {
        location.href = 'http://localhost:8081/html/login.html?regis'
      })
    }

    isCookie () {
      if ( document.cookie ) {
        $('#log').hide()
        $('#reg').hide()
        $('#log1').show()
        $('#reg1').show()
        $('#log1').html('欢迎' + document.cookie.split('=')[1])
        $('#reg1').html('注销')
      } else {
        $('#log1').hide()
        $('#reg1').hide()
        $('#log').show()
        $('#reg').show()
      }
    }

    logout () {
      $('#reg1').on('click', () => {
        let date = new Date();
        date.setTime(date.getTime() - 1);
        document.cookie =  `name=''; expires=${date.toGMTString()}; path=/`;
        location.assign('/?0')
      })
    }

    navClick () {
      if ( isNaN(location.search.slice(1,2)) ) {
        $('.firstnav-wrap li').each((index, key) => {
          $(key).removeClass('curr')
        })
      } else {
        $('.firstnav-wrap li').eq(location.search.slice(1,2)).addClass('curr')
      }
      
    }
  }
  return new Header()
});